<?php
/**
 * Minhlog Domain_Mapper 文章标签类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_DataMapper
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Tag.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_DataMapper_Tag extends Domain_DataMapper_Abstract
{
    protected $_tableClassName = 'Domain_Table_Tag';

    protected $_dataMapper = array (
            'tagId'      => 'tag_id',
            'tagName'    => 'tag_name',
    );

    /**
     * 根据文章查询标签
     * @param Zend_Db_Table_Row $article
     * @return object|void
     */
    public function findTagByArticle(Zend_Db_Table_Row $article)
    {
        if(null !== $article){
            $tags = array();
            $tagSet = $article->findManyToManyRowset('Domain_Table_Tag','Domain_Table_Tag_Article','Article');
            if($tagSet->count()){
                foreach($tagSet as $tag){
                    $articleTag = $this->mapper($tag);
                    $tags[] = $articleTag;
                }
            }
            $resultSet = $tags;
        }else{
            $resultSet = null;
        }
        return $resultSet;
    }

    /**
     * 根据多篇文章查询标签
     * @param Zend_Db_Table_Rowset $articleSet
     * @return object|void
     */
    public function findTagByArticles(Zend_Db_Table_Rowset $articleSet)
    {
        if($articleSet->count()){
            $tags = array();
            foreach($articleSet as $article){
                $tagSet = $article->findManyToManyRowset('Domain_Table_Tag','Domain_Table_Tag_Article','Article');
                if($tagSet->count()){
                    foreach($tagSet as $tag){
                        $articleTag = $this->mapper($tag);
                        $tags[$article->article_id][] = $articleTag;
                    }
                }
            }
            $resultSet = $tags;
        }else{
            $resultSet = null;
        }
        return $resultSet;
    }

    /**
     * 查询全部标签
     * @return object|void
     */
    public function findAllTag()
    {
        return $this->findAll($this->findAllTagStatement());
    }

    /**
     * 封装查询全部标签信息SQL对象
     * @return Zend_Db_Table_Select
     */
    private function findAllTagStatement()
    {
        $select = $this->getTable()->select();
        $select->order('tag_id DESC');
        return $select;
    }
}